package com.example.myapplication;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.material.snackbar.Snackbar;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText userName, password;
    private Map<String, String> accountDB = new HashMap<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnLogin = findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(this);

        userName = findViewById(R.id.user_tv);
        password = findViewById(R.id.pwd_tv);
        accountDB.put("user0", "000000");
        accountDB.put("user1", "111111");
        accountDB.put("user2", "222222");
        accountDB.put("user3", "333333");
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.btn_login) {

            String  nameStr = userName.getText().toString();
            String  pwd = password.getText().toString();



            new AlertDialog.Builder(this)
                    .setTitle("提示")
                    .setIcon(R.drawable.ic_warning)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //TODO 点击了确定
                        }
                    })
                    .setMessage("自定义内容。。。。。。。。")
                    .setCancelable(false)
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //TODO 点击了取消
                        }
                    }).create().show();



            if(accountDB.containsKey(nameStr)) {
                if (accountDB.get(nameStr).equals(pwd)) {
                    //TODO 提示登录成功
                    Toast.makeText(getApplication(), "登录成功", Toast.LENGTH_SHORT).show();
                    Snackbar.make(view,"登录成功", Snackbar.LENGTH_SHORT).show();
                    return;
                }
            }
            //TODO 提示登录失败
            Toast.makeText(getApplication(), "登录失败", Toast.LENGTH_SHORT).show();
            Snackbar.make(view,"登录失败", Snackbar.LENGTH_SHORT).show();
        }
    }
}
